package cn.edu.usst.cs.goldfish.booking.meetingroom.web.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminLoginController {

    @Value("${admin.username}")
    private String adminUsername;

    @Value("${admin.password}")
    private String adminPassword;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        if (adminUsername.equals(username) && adminPassword.equals(password)) {
            session.setAttribute("isAdmin", true);
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("message", "账号或密码错误");
        }
        return result;
    }

    @PostMapping("/logout")
    public Map<String, Object> logout(HttpSession session) {
        session.removeAttribute("isAdmin");
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/check")
    public Map<String, Object> check(@SessionAttribute(value = "isAdmin", required = false) Boolean isAdmin) {
        Map<String, Object> result = new HashMap<>();
        result.put("isAdmin", Boolean.TRUE.equals(isAdmin));
        return result;
    }
}
